<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="fWarnCode">
        <el-input v-model="queryParams.fWarnCode" placeholder="请输入报警编码" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="报警日期">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="fId" />
      <el-table-column label="报警编码" align="center" prop="fWarnCode" />
      <el-table-column label="工厂名称" align="center" prop="bcmOrg.fOrgName" />
      <el-table-column label="设备名称" align="center" prop="wfDevice.fDeviceName" />
      <el-table-column label="报警位置" align="center" prop="fWarnType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wf_warn_type" :value="scope.row.fWarnType" />
        </template>
      </el-table-column>
      <el-table-column label="严重程度" align="center" prop="fWarnStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wf_warn_status" :value="scope.row.fWarnStatus" />
        </template>
      </el-table-column>
      <el-table-column label="是否通知" align="center" prop="fIsNotice">
        <template slot-scope="scope">
          <span>{{ scope.row.fIsNotice === 0 ? '否' : '是' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报警时间" align="center" prop="fWarnTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fWarnTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否处理" align="center" prop="fIsTackle">
        <template slot-scope="scope">
          <span>{{ scope.row.fIsTackle === 0 ? '否' : '是' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-share" @click="handleUpdate(scope.row, 1)"
            v-hasPermi="['system:role:add']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:remove']" v-if="scope.row.fIsTackle !== 1">处理</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改报警记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item prop="configName">
          <strong>报警信息</strong>
          <el-divider />
          <el-row>
            <el-col :span="6">
              <el-form-item label="报警编码">
                <span>{{ form.fWarnCode }}</span>
              </el-form-item>
              <el-form-item label="严重程度">
                <span>{{
                  form.fWarnStatus === 0 ? '一般' :
                    form.fWarnStatus === 1 ? '警告' :
                      form.fWarnStatus === 2 ? '非常严重' :
                        '' }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="工厂名称">
                <span>{{ form.bcmOrg == undefined ? "" : form.bcmOrg.fOrgName }}</span>
              </el-form-item>
              <el-form-item label="是否通知">
                <span>{{ form.fIsNotice === 0 ? '否' : '是' }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="设备名称">
                <span>{{ form.wfDevice == undefined ? "" : form.wfDevice.fDeviceName }}</span>
              </el-form-item>
              <el-form-item label="报警时间">
                <span>{{ form.fWarnTime }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="报警位置">
                <span>{{
                  form.fWarnType === 0 ? '机械臂' :
                    form.fWarnType === 1 ? '驱动板' :
                      form.fWarnType === 2 ? '温箱' :
                        '' }}</span>
              </el-form-item>
              <el-form-item label="报警内容">
                <span>{{ form.fWarnContent }}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="configName">
          <strong>处理信息</strong>
          <el-divider />
          <el-row>
            <el-col :span="8">
              <el-form-item label="处理人" prop="nickName">
                <el-input placeholder="请输入内容" :value="form.sysUser == undefined ? '' : form.sysUser.nickName"
                  :disabled="true" />
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="处理时间" prop="fTackleTime">
                <el-date-picker v-model="form.fTackleTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择日期时间" :disabled="isEdit == 1" />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否处理" prop="fIsTackle">
                <el-radio-group v-model="form.fIsTackle" :disabled="isEdit == 1">
                  <el-radio v-for="dict in fIsTackle" :key="dict.value" :label="dict.value">{{ dict.label
                    }}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row><br>
          <el-row>
            <el-col :span="20">
              <el-form-item label="处理详情" prop="fTackleContent">
                <el-input type="textarea" v-model="form.fTackleContent" :disabled="isEdit == 1"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isEdit != 1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAbn, getAbn, addAbn, updateAbn } from "@/api/abnormal/abnormalData";
export default {
  name: "Abn",
  dicts: ['sys_yes_no', 'wf_warn_status', 'wf_warn_type'],
  data() {
    return {
      dateRange: "",
      isEdit: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报警记录表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fWarnCode: null,
        fOrgId: null,

      },
      // 表单参数
      form: {
        sysUser: {
          nickName: '',
        },
        bcmOrg: {
          fOrgName: '',
        },
        wfDevice: {
          fDeviceName: '',
        },
      },
      // 表单校验
      rules: {
        fWarnCode: [
          { required: true, message: "报警编码不能为空", trigger: "blur" }
        ],
        fIsTackle: [
          { required: true, message: "请选择是否启用不能选空", trigger: "change" }
        ],
      },
      fIsTackle: [
        { value: 1, label: '是' },
        { value: 0, label: '否' },
       
      ],
      wf_warn_status: [
        { value: 0, label: '一般' },
        { value: 1, label: '警告' },
        { value: 2, label: '非常严重' }
      ],
      wf_warn_type: [
        { value: 0, label: '机械臂' },
        { value: 1, label: '驱动板' },
        { value: 2, label: '温箱' }
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    addDateRange(params, dateRange) {
      params.fCreateDateStart = dateRange[0];
      params.fCreateDateEnd = dateRange[1];
      return params;
    },
    /** 查询报警记录列表 */
    getList() {
      this.loading = true;
      listAbn(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fId: null,
        fWarnCode: null,
        fOrgId: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isEdit = 0
      this.reset();
      this.open = true;
      this.title = "添加报警记录";
    },
    /** 查看处理按钮操作 */
    handleUpdate(row, type) {
      this.isEdit = type
      this.reset();
      const fId = row.fId || this.ids
      getAbn(fId).then(response => {
        this.form = response.data;
        this.open = true;
        if (type === 1) {
          this.open = true;
          this.title = "查看报警记录";
        } else {
          this.open = true;
          this.title = "处理报警记录";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fId != null) {
            updateAbn(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAbn(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fIds = row.fId || this.ids;
      this.$modal.confirm('是否确认删除报警记录编号为"' + fIds + '"的数据项？').then(function () {
        return Abn(fIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pam//warn/log/export', {
        ...this.queryParams
      }, `/warn/log_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
